//
//  EGMBleDelegate.h
//  BleHrm
//
//  Created by hh on 14-3-26.
//  Copyright (c) 2014年 egoman. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

#define NOTIFICATION_BLE_WRITE_REQUEST @"didReceiveWriteRequests"
#define KEY_BLE_WRITE_REQUEST_DATA @"BleWriteRequestDataKey"

#define NOTIFICATION_BLE_RSSI @"bleRssiNotification"
#define KEY_BLE_RSSI @"BleRssiKey"

#define NOTIFICATION_BLE_CHARACTER_UPDATED @"bleCharacterUpdatedNotification"
#define KEY_BLE_CHARACTER_VALUE @"BleCharacterValueKey"
#define KEY_BLE_CHARACTER_UUID @"BleCharacterUUIDKey"

#define CENTRAL_MANAGER_IDENTIFIER @"kCentralManagerIdentifier"
#define PERIPHERAL_MANAGER_IDENTIFIER @"kPeripheralManagerIdentifier"


@protocol EGMBleManagerDelagate;

@interface EGMBleManager : NSObject <CBCentralManagerDelegate,CBPeripheralDelegate,CBPeripheralManagerDelegate>

@property(weak,nonatomic) id<EGMBleManagerDelagate> delegate;
@property(strong, nonatomic) CBPeripheral* discoveredPeripheral;
@property(strong,nonatomic) CBCentralManager* centralManager;
@property(strong,nonatomic) CBPeripheralManager* peripheralManager;

@property(nonatomic) BOOL isCleaningUp;
@property(nonatomic) NSInteger notifyCountForCleanUp;

+(EGMBleManager*)sharedBleManager;
+(EGMBleManager *)initWithDelegate:(id<EGMBleManagerDelagate>)delegate;
+(EGMBleManager *)initWithDelegate:(id<EGMBleManagerDelagate>)delegate autoDiscoverServices:(BOOL)autoDiscoverServices;
+(EGMBleManager *)initWithDelegate:(id<EGMBleManagerDelagate>)delegate autoConnectDevice:(BOOL) autoConnectDevice autoDiscoverServices:(BOOL)autoDiscoverServices;
+(EGMBleManager *)initWithDelegate:(id<EGMBleManagerDelagate>)delegate initGattServer:(BOOL)initGattServer;
+(EGMBleManager *)initWithDelegate:(id<EGMBleManagerDelagate>)delegate autoConnectDevice:(BOOL) autoConnectDevice autoDiscoverServices:(BOOL)autoDiscoverServices initGattServer:(BOOL)initGattServer;

- (void)openBluetoothWhenNeeded;
-(void)initCentralManager;
- (void)startScanWithService:(NSArray*)services options:(NSDictionary*)options;
- (void)stopScan;
-(void)disconnect;
-(void)readCharacteristic:(NSString *)characUUID forService:(NSString *)serviceUUID;
-(void)writeCharacteristicValue:(NSData*)data forCharacteristic:(NSString *)characUUID forService:(NSString *)serviceUUID;
-(void)writeCharacteristicValueTip:(NSData*)data forCharacteristic:(NSString *)characUUID forService:(NSString *)serviceUUID;

-(void)setNotifyValue:(BOOL)notifyValue forCharacteristic:(NSString *)characUUID forService:(NSString *)serviceUUID;
-(BOOL)isBluetoothOn;
-(void)discoverServices:(NSArray*)services;
-(BOOL)isConnected;
- (void)cleanup;
-(void)writeCharacteristicValue:(NSData*)data forCharacteristic:(NSString *)characUUID forService:(NSString *)serviceUUID writeType:(CBCharacteristicWriteType)type;
-(void)initGattServer;
-(void)stopGattServer;
-(void)connect;
-(void)restorePeripheralManager:(NSString*)pheripheralManagerIdentifier;
-(void)restoreCentralManager:(NSString*)centralManagerIndentifier;

-(BOOL)haveService:(NSString*)serviceUUID;
@end



//相关代理
@protocol EGMBleManagerDelagate <NSObject>

@optional
-(void)handleMsgConnected;
//-(void)handleMsgReady;
-(void)handleMsgDisconnected;
-(void)handleMsgCharacteristicChanged:(CBUUID*)charUUID value:(NSData*)data;
-(void)handleMsgCharacteristicDiscovered:(CBCharacteristic *)characteristic;
-(void)handleMsgCharacteristicBeganNotify:(CBUUID*)charUUID;
-(void)handleMsgCharacteristicWrited:(CBCharacteristic *)characteristic;

-(void)handleMsgReceivedWriteRequest:(CBUUID*)charUUID value:(NSData*)data;
-(void)handleMsgRssiReaded:(NSInteger)rssi;
-(NSArray*)servicesOfGattServer;


-(void)handleMsgBtPowerOff; //蓝牙断开
-(void)handleMsgBtPowerOn;//蓝牙开启
-(void)handleMsgBtResetting;//蓝牙重置
-(void)handleMsgBtStateChanged;
-(NSString*)restrictedDeviceName;
-(NSArray*)restrictedDeviceNameArray;
-(NSArray*)restrictedServiceUUIDsForDiscover;
//-(NSArray*)restrictedCharacteristicUUIDsForDiscover;
//-(NSArray*)restrictedServiceUUIDsForScan;

-(NSInteger)notifyCountForCleanUp;
-(void)handleMsgServiceAdded:(CBUUID*)serviceUUID;
-(void)handleMsgDeviceFound:(CBPeripheral*)peripheral advData:(NSDictionary*)advData rssi:(NSInteger)rssi;

-(void)handleMsgUserCancelPairing;
-(void)handleMsgWritingNotPermitted:(CBCharacteristic *)characteristic;
@end
